package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * 
 * @param dishDTO
     * @return 
     * @author heiheiheng
     * @create 2025/7/17
     **/
    @PostMapping
    public Result addDish(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.addDish(dishDTO);
        //缓存优化，清理缓存
        redisTemplate.delete("dish_"+dishDTO.getCategoryId());
        return Result.success();
    }

    /**
     * 分页查询菜品列表
     * 
 * @param dishPageQueryDTO
     * @return 
     * @author heiheiheng
     * @create 2025/7/18
     **/
    @GetMapping("/page")
    @ApiOperation("分页查询菜品列表")
    public Result<PageResult> pageDishQuery(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageDishQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除菜品
     * 
 * @param ids
     * @return 
     * @author heiheiheng
     * @create 2025/7/18
     **/
    @DeleteMapping
    @ApiOperation("删除菜品")
    @CacheEvict(cacheNames = "DishCache", allEntries = true)
    public Result deleteDish(@RequestParam List<Long> ids) {
        log.info("删除菜品：：{}",ids);
        dishService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * 
 * @param id
     * @return 
     * @author heiheiheng
     * @create 2025/8/2
     **/
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result getById(@PathVariable Long id) {
        log.info("回显菜单，{}",id);
        DishVO  dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * 
 * @param dishDTO
     * @return 
     * @author heiheiheng
     * @create 2025/8/2
     **/
    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "DishCache",key = "#dishDTO.categoryId")
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}",dishDTO);
        dishService.updateDish(dishDTO);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     *
 * @param categoryId
     * @return
     * @author heiheiheng
     * @create 2025/8/2
     **/
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<ArrayList> getDishByCategoryId(Long categoryId) {
        log.info("根据分类id查询菜品：{}",categoryId);
        ArrayList<Dish> dishByCategoryId = dishService.getDishByCategoryId(categoryId);
        return Result.success(dishByCategoryId);
    }

    /**
     * 修改菜品状态
     *
 * @param dishDTO
     * @return
     * @author heiheiheng
     * @create 2025/8/2
     **/
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    @CacheEvict(cacheNames = "DishCache",allEntries = true)
    public Result dishStartOrStop(DishDTO dishDTO) {
        log.info("修改菜品状态：{}",dishDTO);
        dishService.dishStartOrStop(dishDTO);
        return Result.success();
    }
}
